//
//  BHMessageMainViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/19.
//

import UIKit

class BHMessageMainViewController: BHBaseViewController {

    lazy var gourpView: BHMessageMainHeaderView = {
        let view = BHMessageMainHeaderView.loadFromXib()
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: 110)
        return view
    }()
    
    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .grouped)
        view.frame = CGRect(x: 0, y: UIScreen.begainY+110, width: UIScreen.width, height: UIScreen.showHeight-110)
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .white
        view.register(UINib.init(nibName: "BHMessageLatestMessageCell", bundle: nil), forCellReuseIdentifier: "BHMessageLatestMessageCell")
        view.register(TUIConversationCell.self, forCellReuseIdentifier: kConversationCell_ReuseId)
        return view
    }()
    
    lazy var kConversationCell_ReuseId: String = "TConversationCell"
    
    lazy var viewModel: TConversationListViewModel = {
        let model = TConversationListViewModel.init()
        model.delegate = self
        return model
    }()
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.viewModel.loadConversation()
        
        self.bh_navigationBar.title_label.text = "消息"
        self.view.addSubview(self.gourpView)
    
        self.view.addSubview(self.tableView)
        
        self.gourpView.activityGroupBtn.addBtnAction(action: { (_) in
            
           
        }, for: .touchUpInside)
        
        
    }
}

extension BHMessageMainViewController: TConversationListViewModelDelegate {
    func refreshNewMessage() {
        self.tableView.reloadData()
    }
}

extension BHMessageMainViewController: UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return self.viewModel.dataList.count
//        return 0
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 76
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            tableView.beginUpdates()

            let conv = self.viewModel.dataList[indexPath.row]
            self.viewModel.remove(conv)
            tableView.deleteRows(at: [indexPath], with: .none)

            tableView.endUpdates()
        }
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
  
}

extension BHMessageMainViewController: UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 10))
        view.backgroundColor = .color_f8f8f8
        return view
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: self.kConversationCell_ReuseId, for: indexPath) as! TUIConversationCell
        
        let conv = self.viewModel.dataList[indexPath.row]
        conv.cselector = #selector(didSelectConversation(cell:));
        cell.fill(with: conv)
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == (self.viewModel.dataList.count-1) {
            cell.separatorInset = .zero
        }else {
            cell.separatorInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        }
        
    }
    
    @objc func didSelectConversation(cell: TUIConversationCell) {
        let conversation = cell.convData
        BHRouter.shared.bh_goChat(data: conversation)
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     
    }
}
